import PySimpleGUI as sg
import cv2
import sys,time
from pathlib import Path

try:
    media_file = sys.argv[1]
except:
    media_file = '../1.mp4'

mediaFileName = ''.join([str(Path(media_file).name),str(Path(media_file).suffix)])
mediaFileName = ''.join(['analysing ',str(Path(media_file).name)])

sg.ChangeLookAndFeel('GreenTan')
text_elem = sg.Text(size=(18,1))

layout = [
  [sg.Text("Analyse by following opencv functions.")],
  [sg.Image(filename='', key='image')],
  [text_elem],
  [sg.Button('Pause', size=(50, 1)),
   #sg.Button('Continue', size=(33, 1)),
   sg.Button('Stop', size=(50, 1))],
  [sg.Radio('None', 'Radio', True, size=(10, 1))],
  [sg.Radio('threshold', 'Radio', size=(10, 1), key='thresh'),
   sg.Slider((0, 255), 128, 1, orientation='h', size=(40, 15), key='thresh_slider')],
  [sg.Radio('canny', 'Radio', size=(10, 1), key='canny'),
   sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='canny_slider_a'),
   sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='canny_slider_b')],
  [sg.Radio('contour', 'Radio', size=(10, 1), key='contour'),
   sg.Slider((0, 255), 128, 1, orientation='h', size=(20, 15), key='contour_slider'),
   sg.Slider((0, 255), 80, 1, orientation='h', size=(20, 15), key='base_slider')],
  [sg.Radio('blur', 'Radio', size=(10, 1), key='blur'),
   sg.Slider((1, 11), 1, 1, orientation='h', size=(40, 15), key='blur_slider')],
  [sg.Radio('hue', 'Radio', size=(10, 1), key='hue'),
   sg.Slider((0, 225), 0, 1, orientation='h', size=(40, 15), key='hue_slider')],
  [sg.Radio('enhance', 'Radio', size=(10, 1), key='enhance'),
   sg.Slider((1, 255), 128, 1, orientation='h', size=(40, 15), key='enhance_slider')],
]

window = sg.Window(mediaFileName,
           layout,
           location=(10, 10),
           return_keyboard_events=True,
           use_default_focus=False,
           finalize=True)

width = 640
height = 480

def on_key_press(event):
    if event.key in "rgbcmyk":
        line.set_color(event.key)
    elif event.key == 'q':
        sys.exit()
    fig.canvas.draw_idle()

cap = cv2.VideoCapture(media_file)
while True:
    event, values = window.read(timeout=0, timeout_key='timeout')

    ret, frame = cap.read()
    try:
        frame = cv2.resize(frame,(width,height))
    except:
        break
    
    if values['thresh']:
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)[:, :, 0]
        frame = cv2.threshold(frame, values['thresh_slider'], 255, cv2.THRESH_BINARY)[1]
    if values['canny']:
        frame = cv2.Canny(frame, values['canny_slider_a'], values['canny_slider_b'])
    if values['contour']:
        hue = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        hue = cv2.GaussianBlur(hue, (21, 21), 1)
        hue = cv2.inRange(hue, np.array([values['contour_slider'], values['base_slider'], 40]),
                          np.array([values['contour_slider'] + 30, 255, 220]))
        cnts= cv2.findContours(hue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
        cv2.drawContours(frame, cnts, -1, (0, 0, 255), 2)
    if values['blur']:
        frame = cv2.GaussianBlur(frame, (21, 21), values['blur_slider'])
    if values['hue']:
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        frame[:, :, 0] += int(values['hue_slider'])
        frame = cv2.cvtColor(frame, cv2.COLOR_HSV2BGR)
    if values['enhance']:
        enh_val = values['enhance_slider'] / 40
        clahe = cv2.createCLAHE(clipLimit=enh_val, tileGridSize=(8, 8))
        lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
        lab[:, :, 0] = clahe.apply(lab[:, :, 0])
        frame = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
    if event == 'Pause' or event is None:
        position = ret
        imgBuffer = cv2.imencode('.png',frame)[1].tobytes()
        time.sleep(2) 
    if event == 'Continue' or event is None:
        try:
            ret = position 
        except:
            ret = ret
        position = 0
    if event == 'Stop' or event is None:
        break
    imgbytes = cv2.imencode('.png', frame)[1].tobytes()
    window['image'].update(data=imgbytes)
    text_elem.update(event)

window.close()
